using System;

namespace OracleInternal.Network
{
	internal class NVNavigator
	{
		private string m_SearchName;

		private int m_cursor;

		private NVPair m_nvp;

		internal NVNavigator(NVPair nvp)
		{
			m_nvp = nvp;
		}

		internal static NVPair FindNVPairRecurse(NVPair nvp, string name)
		{
			if (nvp == null || string.Equals(name, nvp.Name, StringComparison.InvariantCultureIgnoreCase))
			{
				return nvp;
			}
			if (nvp.RHSType == NVPair.RHS_ATOM)
			{
				return null;
			}
			for (int i = 0; i < nvp.ListSize; i++)
			{
				NVPair nVPair = FindNVPairRecurse(nvp.GetListElement(i), name);
				if (nVPair != null)
				{
					return nVPair;
				}
			}
			return null;
		}

		internal void SetFindString(string name)
		{
			m_SearchName = name;
			m_cursor = 0;
		}

		internal NVPair FindNVPair()
		{
			NVPair nVPair = null;
			if (m_SearchName == null || m_nvp == null || m_nvp.RHSType != NVPair.RHS_LIST)
			{
				return null;
			}
			while (m_cursor < m_nvp.ListSize && nVPair == null)
			{
				NVPair listElement = m_nvp.GetListElement(m_cursor);
				if (string.Equals(m_SearchName, listElement.Name, StringComparison.InvariantCultureIgnoreCase))
				{
					nVPair = listElement;
				}
				m_cursor++;
			}
			if (m_cursor == m_nvp.ListSize)
			{
				m_cursor = 0;
				m_SearchName = null;
			}
			return nVPair;
		}

		internal static NVPair FindNVPair(NVPair nvp, string name)
		{
			if (nvp == null || nvp.RHSType != NVPair.RHS_LIST)
			{
				return null;
			}
			for (int i = 0; i < nvp.ListSize; i++)
			{
				NVPair listElement = nvp.GetListElement(i);
				if (string.Equals(name, listElement.Name, StringComparison.InvariantCultureIgnoreCase))
				{
					return listElement;
				}
			}
			return null;
		}

		internal static NVPair FindNVPair(NVPair nvp, string[] names)
		{
			NVPair nVPair = nvp;
			for (int i = 0; i < names.Length; i++)
			{
				nVPair = FindNVPair(nVPair, names[i]);
				if (nVPair == null)
				{
					return null;
				}
			}
			return nVPair;
		}
	}
}
